package com.ld.admin.controller.system;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ld.admin.controller.AdminBasicController;
import com.ld.admin.model.system.SysMenuModel;
import com.ld.common.util.cache.CacheManager;
import com.ld.common.util.cache.CacheModel;
import com.ld.shieldsb.common.web.util.Web;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * 缓存管理
 * 
 * @ClassName SysCacheController
 * @author <a href="mailto:donggongai@126.com" target="_blank">吕凯</a>
 * @date 2019年01月14日 11:31:54
 *
 */
@Api(tags = "缓存管理")
@ConditionalOnProperty(prefix = "modules.sc.system", name = "cache.enable", havingValue = "true", matchIfMissing = true)
@Controller(value = "systemSysCacheController")
@RequestMapping("${shieldsb.adminPath}/system/cache")
public class SysCacheController extends AdminBasicController<SysMenuModel> {

    @ApiOperation(value = "首页", notes = "")
    @RequestMapping(path = "", method = RequestMethod.GET)
    public String index() {
        Map<String, CacheModel> cacheMap = CacheManager.getAllCacheModels();
        List<CacheModel> cacheList = new ArrayList<>();
        cacheMap.keySet().stream().forEach(key -> {
            cacheList.add(cacheMap.get(key));
        });
        request.setAttribute("list", cacheList);
        return "a/system/cache/cache_list";
    }

    @ApiOperation(value = "查看缓存", notes = "")
    @RequestMapping(path = "/show", method = RequestMethod.GET)
    public String show() {
        String keyCode = getString("keyCode");
        CacheModel cacheModel = CacheManager.get(keyCode);
        request.setAttribute("model", cacheModel);
        return "a/system/cache/cache_show";
    }

    @ApiOperation(value = "刷新缓存", notes = "")
    @RequestMapping(path = "/refresh", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    public Object refresh() {
        String keyCode = getString("keyCode");
        CacheModel cacheModel = CacheManager.get(keyCode);
        Boolean result = cacheModel.reload();
        if (result) {
            return Web.Response.success("刷新缓存成功！");
        } else {
            return Web.Response.error("刷新缓存失败！");
        }
    }

    @ApiOperation(value = "清空缓存", notes = "")
    @RequestMapping(path = "/clear", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    public Object clear() {
        String keyCode = getString("keyCode");
        CacheModel cacheModel = CacheManager.get(keyCode);
        Boolean result = cacheModel.clear();
        if (result) {
            return Web.Response.success("清空缓存成功！");
        } else {
            return Web.Response.error("清空缓存失败！");
        }
    }

}